package com.hiultra.archive.controller;

import com.hiultra.archive.service.IArcCabinetService;
import com.hiultra.archive.service.IArcShelvesService;
import com.hiultra.archive.service.IArcStorehouseService;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.constant.UserConstants;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.core.domain.entity.ArcCabinet;
import com.hiultra.common.core.domain.entity.ArcShelves;
import com.hiultra.common.core.domain.entity.ArcStorehouse;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.common.utils.StringUtils;
import com.hiultra.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 库房Controller
 *
 * @author ls
 * @date 2022-08-22
 */
@Api(tags="库房Controller")
@RestController
@RequestMapping("/deposit/storehouse")
public class ArcStorehouseController extends BaseController {
    @Autowired
    private IArcStorehouseService arcStorehouseService;
    @Autowired
    private IArcCabinetService arcCabinetService;
    @Autowired
    private IArcShelvesService arcShelvesService;

    /**
     * 查询库房列表
     */
    @ApiOperation(value = "查询库房列表", notes = "查询库房列表")
    @PreAuthorize("@ss.hasPermi('deposit:storehouse:list')")
    @GetMapping("/list")
    public AjaxResult list(ArcStorehouse arcStorehouse) {
        List<ArcStorehouse> list = arcStorehouseService.selectArcStorehouseList(arcStorehouse);
        return AjaxResult.success(list);
    }

    /**
     * 导出库房列表
     */
    @ApiOperation(value = "导出库房列表", notes = "导出库房列表")
    @PreAuthorize("@ss.hasPermi('deposit:storehouse:export')")
    @Log(title = "库房", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArcStorehouse arcStorehouse) {
        List<ArcStorehouse> list = arcStorehouseService.selectArcStorehouseList(arcStorehouse);
        ExcelUtil<ArcStorehouse> util = new ExcelUtil<ArcStorehouse>(ArcStorehouse.class);
        util.exportExcel(response, list, "库房数据");
    }

    /**
     * 获取库房详细信息
     */
    @ApiOperation(value = "获取库房详细信息", notes = "获取库房详细信息")
    @PreAuthorize("@ss.hasPermi('deposit:storehouse:query')")
    @GetMapping(value = "/{storehouseId}")
    public AjaxResult getInfo(@PathVariable("storehouseId") Long storehouseId) {
        return AjaxResult.success(arcStorehouseService.selectArcStorehouseByStorehouseId(storehouseId));
    }

    /**
     * 新增库房
     */
    @ApiOperation(value = "新增库房", notes = "新增库房")
    @PreAuthorize("@ss.hasPermi('deposit:storehouse:add')")
    @Log(title = "库房", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArcStorehouse arcStorehouse) {
        if (UserConstants.NOT_UNIQUE.equals(arcStorehouseService.checkStorehouseNameUnique(arcStorehouse))) {
            return AjaxResult.error("新增库房'" + arcStorehouse.getStorehouseName() + "'失败，库房名称已存在");
        }
        arcStorehouse.setCreateBy(getUsername());
        return toAjax(arcStorehouseService.insertArcStorehouse(arcStorehouse));
    }

    /**
     * 修改库房
     */
    @ApiOperation(value = "修改库房", notes = "修改库房")
    @PreAuthorize("@ss.hasPermi('deposit:storehouse:edit')")
    @Log(title = "库房", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArcStorehouse arcStorehouse) {
        Long storehouseId = arcStorehouse.getStorehouseId();
        if (UserConstants.NOT_UNIQUE.equals(arcStorehouseService.checkStorehouseNameUnique(arcStorehouse))) {
            return AjaxResult.error("修改库房'" + arcStorehouse.getStorehouseName() + "'失败，库房名称已存在");
        } else if (arcStorehouse.getParentId().equals(storehouseId)) {
            return AjaxResult.error("修改部门'" + arcStorehouse.getStorehouseName() + "'失败，上级库房不能是自己");
        }
//        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0)
//        {
//            return AjaxResult.error("该部门包含未停用的子部门！");
//        }
        arcStorehouse.setUpdateBy(getUsername());

        return toAjax(arcStorehouseService.updateArcStorehouse(arcStorehouse));
    }

    /**
     * 删除库房
     */
    @ApiOperation(value = "删除库房", notes = "删除库房")
    @PreAuthorize("@ss.hasPermi('deposit:storehouse:remove')")
    @Log(title = "库房", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storehouseIds}")
    public AjaxResult remove(@PathVariable Long[] storehouseIds) {
        //查询是否被引用智能柜和密集架
        for (Long storehouse : storehouseIds) {
            List<ArcCabinet> arcCabinetList = arcCabinetService.selectArcCabinetList(new ArcCabinet() {{
                setStorehouseId(storehouse);
            }});
            if (arcCabinetList.size() > 0) {
                ArcStorehouse arcStorehouse = arcStorehouseService.selectArcStorehouseByStorehouseId(storehouse);
                return AjaxResult.error("库房" + arcStorehouse.getStorehouseName() + "被智能柜信息引用，无法删除！");
            }

            List<ArcShelves> arcShelvesList = arcShelvesService.selectArcShelvesList(new ArcShelves() {{
                setStorehouseId(storehouse);
            }});
            if (arcShelvesList.size() > 0) {
                ArcStorehouse arcStorehouse = arcStorehouseService.selectArcStorehouseByStorehouseId(storehouse);
                return AjaxResult.error("库房" + arcStorehouse.getStorehouseName() + "被密集架信息引用，无法删除！");
            }
        }
        return toAjax(arcStorehouseService.deleteArcStorehouseByStorehouseIds(storehouseIds));
    }

    /**
     * 获取库房下拉树列表
     */
    @ApiOperation(value = "获取库房下拉树列表", notes = "获取库房下拉树列表")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(ArcStorehouse arcStorehouse) {
        List<ArcStorehouse> storehouses = arcStorehouseService.selectArcStorehouseList(arcStorehouse);
        return AjaxResult.success(arcStorehouseService.buildStorehouseTreeSelect(storehouses));
    }
}
